package com.samuel.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.samuel.blog.bean.PageBean;
import com.samuel.blog.dao.BaseDao;
import com.samuel.blog.service.BaseService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseServiceImpl<T, E extends Serializable> implements BaseService<T, E> {

	private static final Logger logger = Logger.getLogger(BaseServiceImpl.class);
	
	@Autowired  
    private BaseDao<T, E> baseDao;  
      
    public BaseDao<T, E> getBaseDao() {  
        return baseDao;  
    }  
    public void setBaseDao(BaseDao<T, E> baseDao) {  
        this.baseDao = baseDao;  
    }  
	
	@Override
	public PageInfo<T> selectPage(PageBean pageBean, Map<String, Object> map) {
		logger.info("page info ==>" + pageBean.toString());
		PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
		List<T> list = baseDao.selectByMap(map);
		PageInfo<T> pageInfo = new PageInfo<T>(list);
		return pageInfo;
	}

	@Override
	public T selectById(E id) {
		return baseDao.selectByPrimaryKey(id);
	}

	@Override
	public int updateById(T entity) {
		return baseDao.updateByPrimaryKeySelective(entity);
	}

	@Override
	public List<T> selectByMap(Map<String, Object> map) {
		if (null == map) {
			map = new HashMap<>();
		}
		List<T> list = baseDao.selectByMap(map);
		return list == null || list.size() <= 0 ? null : list;
	}

	@Override
    public int insert(T entity) {
		return baseDao.insert(entity);
	}
	
	@Override
	public List<T> selectAll() {
		return this.selectByMap(null);
	}

}
